﻿namespace ContactManager.Controllers
{
   using System.Collections.Generic;
   using System.Linq;
   using System.Net;
   using System.Net.Http;
   using System.Web.Http;
   using Models;

   public class ContactsController : ApiController
   {
      private readonly IEnumerable<Contact> contacts = new List<Contact>
                                                          {
                                                             new Contact
                                                                {
                                                                   Id = 1,
                                                                   Name = "Allen Scharfenberg",
                                                                   Email = "aescharfenberg@live.com",
                                                                   Phone = "217-555-8690"
                                                                },
                                                             new Contact
                                                                {
                                                                   Id = 2,
                                                                   Name = "Mark Lovik",
                                                                   Email = "mark.lovik@gmail.com",
                                                                   Phone = "217-555-5877"
                                                                },
                                                             new Contact
                                                                {
                                                                   Id = 3,
                                                                   Name = "Andrew Vermie",
                                                                   Email = "andrew.vermie@gmail.com",
                                                                   Phone = "217-555-7382"
                                                                },
                                                             new Contact
                                                                {
                                                                   Id = 4,
                                                                   Name = "Jose Arce",
                                                                   Email = "jose@hyperionpc.com",
                                                                   Phone = "217-555-2200"
                                                                },
                                                             new Contact
                                                                {
                                                                   Id = 5,
                                                                   Name = "Lee Dumond",
                                                                   Email = "leedumond@gmail.com",
                                                                   Phone = "217-555-2627"
                                                                }
                                                          };

      public IEnumerable<Contact> GetContacts()
      {
         return contacts;
      }

      public Contact GetContactById(int id)
      {
         Contact contact = contacts.FirstOrDefault(c => c.Id == id);

         if (contact == null)
         {
            throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotFound));
         }

         return contact;
      }

      public IEnumerable<Contact> GetContactsByEmailDomain(string domain)
      {
         return contacts.Where(c => c.Email.Contains("@" + domain));
      }
   }
}